//
//  PTSBaseViewController.m
//  PTS80
//
//  Created by 黄杰 on 15/10/31.
//  Copyright © 2015年 黄杰. All rights reserved.
//
 
#import "PTSBaseViewController.h"

@interface PTSBaseViewController () <UIGestureRecognizerDelegate>

@end

@implementation PTSBaseViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    // 设置返回按钮
    [self setupBackBtn];
    
    if (IOS7) {
        self.automaticallyAdjustsScrollViewInsets = NO;
        self.edgesForExtendedLayout = UIRectEdgeAll;
    }
    
}

- (void)setupNavTitle:(NSString *)title withColor:(UIColor *)color
{
    self.title = title;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    // 移除自带的item
    for (UIView *child in self.tabBarController.tabBar.subviews) {
        if ([child isKindOfClass:[UIControl class]]) {
            [child removeFromSuperview];
        }
    }
}

- (void)setupNavImage:(UIImage *)image
{
    [self.navigationController.navigationBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    
}

#pragma mark - 设置返回按钮
- (void)setupBackBtn
{
    if (IOS7) {
        [self.navigationController.navigationBar setBackIndicatorImage:[UIImage imageNamed:@"PTS80.bundle/nav_back_btn"]];
        [self.navigationController.navigationBar setBackIndicatorTransitionMaskImage:[UIImage imageNamed:@"PTS80.bundle/nav_back_btn"]];
        
        UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:nil action:nil];
        self.navigationItem.backBarButtonItem = backItem;
    } else {
        self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@" " style:UIBarButtonItemStyleDone target:nil action:nil];
    }
}

#pragma mark - UIGestureRecognizerDelegate代理方法
-(BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{
    if (!_isPoping) {
        _isPoping = YES;
        return YES;
    }
    return NO;
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRequireFailureOfGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    [otherGestureRecognizer requireGestureRecognizerToFail:gestureRecognizer];
    return NO;
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    _isPoping = NO;
}

#pragma mark - 返回事件
- (void)back
{
    [self.navigationController popToRootViewControllerAnimated:YES];
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    DebugLog(@"%@已经销毁", [self class]);
}

@end
